package org.iuniv.adb.patent.webapp.servlet;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.iuniv.adb.patent.common.Constants;
import org.iuniv.adb.patent.common.StringUtils;
import org.iuniv.adb.patent.model.Patent;
import org.iuniv.adb.patent.service.PatentService;
import org.iuniv.adb.patent.service.PatentServiceImpl;

/**
 * Servlet implementation class for searching Patent
 *
 * @author ctran33
 */
@WebServlet("/patents")
public class PatentSearchServlet extends HttpServlet {

    private static final long serialVersionUID = 3897589990782340102L;
    private static final String P_SRCH_MODE = "sm";
    private static final String P_QUERY = "q";
    private static final String P_PATNUM = "patNum";
    private static final String RESULT = "result";
    private static final String SM_ADV = "1";

    /**
     * @see HttpServlet#HttpServlet()
     */
    public PatentSearchServlet() {
        super();
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // get list of related patents
        try {
            PatentService patentSrv = new PatentServiceImpl();
            String srchMode = request.getParameter(P_SRCH_MODE);
            String dispatcherUrl = null;

            if (SM_ADV.equals(srchMode)) {
                Map<String, Object> mapAdvOpts = new HashMap<String, Object>();
                String advOpt = request.getParameter(Constants.AS_PNUM).trim();
                if (!StringUtils.isEmpty(advOpt)) {
                    mapAdvOpts.put(Constants.AS_PNUM, advOpt);
                }
                advOpt = request.getParameter(Constants.AS_PTITLE);
                if (!StringUtils.isEmpty(advOpt)) {
                    mapAdvOpts.put(Constants.AS_PTITLE, advOpt);
                }
                advOpt = request.getParameter(Constants.AS_PINVENTOR);
                if (!StringUtils.isEmpty(advOpt)) {
                    mapAdvOpts.put(Constants.AS_PINVENTOR, advOpt);
                }
                advOpt = request.getParameter(Constants.AS_PASSIGNEE);
                if (!StringUtils.isEmpty(advOpt)) {
                    mapAdvOpts.put(Constants.AS_PASSIGNEE, advOpt);
                }
                advOpt = request.getParameter(Constants.AS_PUSCLS);
                if (!StringUtils.isEmpty(advOpt)) {
                    mapAdvOpts.put(Constants.AS_PUSCLS, advOpt);
                }
                advOpt = request.getParameter(Constants.AS_PINTLCLS);
                if (!StringUtils.isEmpty(advOpt)) {
                    mapAdvOpts.put(Constants.AS_PINTLCLS, advOpt);
                }
                List<Patent> patentList = patentSrv.findPatentsByAdvOpts(mapAdvOpts);
                request.setAttribute(RESULT, patentList);
                dispatcherUrl = "/patentlist.jsp";
            } else {
                String query = request.getParameter(P_QUERY);
                String patnum = request.getParameter(P_PATNUM);
                if (!StringUtils.isEmpty(query)) {
                	List<Patent> patentList = patentSrv.findPatentsByQuery(query.trim());
                    request.setAttribute(RESULT, patentList);
                    dispatcherUrl = "/patentlist.jsp";
                } else if (!StringUtils.isEmpty(patnum)) {
                    Patent patent = patentSrv.findPatentByNum(patnum.trim());
                    request.setAttribute(RESULT, patent);
                    dispatcherUrl = "/patentdetails.jsp";
                }
            }
            RequestDispatcher rd = request.getRequestDispatcher(dispatcherUrl);
            rd.forward(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            Logger.getLogger(this.getServletName()).log(Level.SEVERE, e.getLocalizedMessage());
            throw new ServletException(e);
        }
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }
}
